/**
 * @Author: wangliuyang
 * @Date: 2021/8/16 10:51
 * @Description: 45. 跳跃游戏 II
 */
public class Solution_45 {
    public int jump(int[] nums) {
        if (nums == null || nums.length == 0){
            return 0;
        }
        int ans = 0;
        int maxRight = 0;//记录当前步数能到达的最右边界
        int end = 0;//下次跳跃后到达的最远位置
        for (int i = 0; i < nums.length - 1; i++) {
            maxRight = Math.max(maxRight,nums[i] + i);
            if (end == i){
                end = maxRight;
                ans++;
            }
        }
        return ans;
    }
}
